import javax.imageio.ImageIO;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class GUI_Main_Menu extends JFrame {
	
	public Container contents;
	private JLabel title;
	private JLabel screenShot;
	private JLabel splash;
	private JButton logIn, createAccount, joinGame, createGame, options, exit;
	
	public GUI_Main_Menu(){
		super("Connect 3 Main Menu");
		
		contents = getContentPane();
		title = new JLabel("Connect 3");
		title.setFont(new Font("Tahoma",Font.PLAIN, 100));
		logIn = new JButton("log in");
		createAccount = new JButton("create an account");
		joinGame = new JButton("join a game");
		createGame = new JButton("create a game");
		options = new JButton("edit game options");
		exit = new JButton("exit");
		
		logIn.setSize(new Dimension(300,6666));
		
		splash = new JLabel(new ImageIcon("images/Connect3.jpg"));
		contents.setLayout(new BorderLayout());
		Box bv = Box.createVerticalBox();		
		
		bv.add(logIn);
		bv.add(createAccount);
		bv.add(joinGame);
		bv.add(createGame);
		bv.add(options);
		bv.add(exit);
		
		contents.add(title, BorderLayout.NORTH);
		contents.add(splash,BorderLayout.WEST);
		contents.add(bv, BorderLayout.EAST);
		
		
		logIn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				logIn();
			}
		});
		
		createAccount.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				createAccount();
			}
		});
		joinGame.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				joinGame();
			}
		});
		
		createGame.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				createGame();
			}
		});
		options.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				options();
			}
		});
		exit.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				System.exit(0);
			}
		});
		pack();
		setResizable(false);
		setVisible(true);
		
	}
	
	public void logIn(){
		GUI_Log_In dialog = new GUI_Log_In(this, true);
		dialog.setVisible(true);
	}
	
	public void createAccount(){
		GUI_Create_Account dialog = new GUI_Create_Account(this, true);
		dialog.setVisible(true);
	}
	
	public void createGame(){
		//run a server
		ServerController.startServer();
		//join your own server
		ClientController cl = new ClientController();
    	
    	if(cl.connect("127.0.0.1", ServerController.PORT_NUMBER)){
    		cl.run();
    		dispose();
    	}else{
    		JOptionPane.showMessageDialog(this,"Could not connect to this ip address.","connection error", JOptionPane.DEFAULT_OPTION);
    	}		
		
	}
	
	public void joinGame(){
		setVisible(false);
		new GUI_Join_A_Game(this).setVisible(true);
	}
	
	public void options(){
		GUI_Server_Options dialog = new GUI_Server_Options(this, true);
		dialog.setVisible(true);
	}
	

}
